package smartcrud.spring.controllers.crud;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import smartcrud.common.orm.Page;
import smartcrud.common.orm.PropertyFilter;
import smartcrud.common.orm.jdbc.JdbcUtils;
import smartcrud.spring.models.crud.World;
import smartcrud.spring.models.crud.WorldService;
/**
 * Rails 1.2中对REST的实现默认把controller中的方法限制在7个：<br>
 * index、show、new、edit、create、 update和destroy.<br>
 * 而根据SpringSide4,使用Restful风格的Urls:<br>
 * 
 * <pre>
 * List page     : GET  /world/ 
 * Show page     : GET  /world/{id}
 * Create page   : GET  /world/create
 * Create action : POST /world/create
 * Update page   : GET  /world/update/{id}
 * Update action : POST /world/update
 * Delete action : GET  /world/delete
 * </pre>
 * 
 * @author cyper.yin
 * 
 */
@Controller
@RequestMapping(value = "/world")
public class WorldController {
    private final static Map<String,String> sexList = new TreeMap<String,String>();
    static{
        sexList.put("1", "男");
        sexList.put("2", "女");
    }
	private static final String SUCCESS = "redirect:/world/";

	@Autowired
	private WorldService worldService;

	@RequestMapping(value = "")
	public String index(
			@RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
			Model model, 
			HttpServletRequest request) {
		
		List<PropertyFilter> filters = JdbcUtils.buildPropertyFilters(request);
		
		Page<World> page = new Page<World>(3);
		page.setPageIndex(pageIndex);
		page = worldService.findByPage(page,filters);
		
		model.addAttribute("page", page);
		
		return "world/world_index";
	}

	@RequestMapping("/{id}")
	public String show(Model model, @PathVariable("id") Long id) {
		World world = worldService.findWorldById(id);
		model.addAttribute(world);

		return "world/world_show";
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(ModelMap model) {
	    model.addAttribute("sexList",sexList);
        
		World world = new World();
		model.addAttribute(world);
		return "world/world_new";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@ModelAttribute("world") World world,
			RedirectAttributes redirectAttributes) {
		worldService.createWorld(world);
		redirectAttributes.addFlashAttribute("message", "create success!");

		return SUCCESS;
	}
	
    @InitBinder
    protected void initBinder(WebDataBinder binder) throws ServletException {
        // binder.registerCustomEditor(byte[].class,
        // new ByteArrayMultipartFileEditor());
        
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }
	
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(Model model, @PathVariable("id") Long id) {
        model.addAttribute("sexList",sexList);
        
		World world = worldService.findWorldById(id);
		model.addAttribute(world);
		return "world/world_edit";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@ModelAttribute("world") World world,
			RedirectAttributes redirectAttributes) {
		worldService.updateWorld(world);
		redirectAttributes.addFlashAttribute("message", "update success!");
		return SUCCESS;
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id,
			RedirectAttributes redirectAttributes) {
		worldService.deleteWorld(id);
		redirectAttributes.addFlashAttribute("message", "delete success!");
		return SUCCESS;
	}
}
